<?php
/**
 * Created by PhpStorm.
 * User: HeiYanHeiMao
 * Date: 2020/2/27
 * Time: 17:30
 */

namespace app\controller;


use app\middleware\Check;
use app\model\CategoryModel;
use think\facade\Validate;
use think\Request;
use think\response\Json;
use think\validate\ValidateRule;

/**商品分类管理
 * Class Category
* @package app\controller
 */
class Category
{
    protected $middleware = [Check::class];

    /**商户获取商品分类列表
     * @param Request $request
     * @return Json
     */
    public function storeGetList(Request $request)
    {
        if (!$request->isPost()) return jsonFail('非法请求方式');
        // 逻辑处理
        return CategoryModel::storeGetList();
    }

    /**列表
     * @param Request $request
     * @return Json
     */
    public function list(Request $request)
    {
        if (!$request->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['limit'] = $request->post('limit');//每页显示数量
        $input['page'] = $request->post('page');//当前页
        // 参数校验
        $validate = Validate::rule([
            'limit' => ValidateRule::isRequire(null, '缺少参数'),
            'page' => ValidateRule::isRequire(null, '缺少参数')
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return CategoryModel::list($input, $request->post('token'));
    }

    /**添加
     * @param Request $request
     * @return Json
     */
    public function add(Request $request)
    {
        if (!$request->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['name'] = $request->post('name');
        $input['pic'] = $request->post('pic');
        $input['type'] = $request->post('type');
        $input['sort'] = $request->post('sort');
        // 参数校验
        $validate = Validate::rule([
            'pic' => ValidateRule::isRequire(null, '缺少参数'),
            'name' => ValidateRule::isRequire(null, '缺少参数'),
            'type' => function($value) {
                return in_array($value, ['1', '2'], true) ? true : '不合法参数';
            },
            'sort' => function($value) {
                return $value >= 1 && $value <= 255 ? true : '不合法参数';
            },
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return CategoryModel::add($input, $request->post('token'));
    }

    /**获取单个
     * @param Request $request
     * @return Json
     */
    public function getOnly(Request $request)
    {
        if (!$request->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['id'] = $request->post('id');
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(null, '缺少参数')
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return CategoryModel::getOnly($input, $request->post('token'));
    }

    /**编辑
     * @param Request $request
     * @return Json
     */
    public function edit(Request $request)
    {
        if (!$request->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['id'] = $request->post('id');
        $input['name'] = $request->post('name');
        $input['pic'] = $request->post('pic');
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(null, '缺少参数'),
            'pic' => ValidateRule::isRequire(null, '缺少参数'),
            'name' => ValidateRule::isRequire(null, '缺少参数')
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return CategoryModel::edit($input, $request->post('token'));
    }

    /**编辑排序
     * @param Request $request
     * @return Json
     */
    public function editSort(Request $request)
    {
        if (!$request->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['id'] = $request->post('id');
        $input['sort'] = $request->post('sort');
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(null, '缺少参数'),
            'sort' => function($value) {
                return $value >= 1 && $value <= 255 ? true : '不合法参数';
            },
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return CategoryModel::editSort($input, $request->post('token'));
    }
    /**删除
     * @param Request $request
     * @return Json
     */
    public function delete(Request $request)
    {
        if (!$request->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['id'] = $request->post('id');
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(null, '缺少参数')
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return CategoryModel::delete($input, $request->post('token'));
    }
}